<?php
//require('Header_iframe.php');
include('../db_connect.php');

$add_course=true;

$str_length=strlen($_POST['studentULinkID']);
if($str_length<6 || $str_length >10){
	$add_course=false;
    echo "Sorry, could not validate student ULink ID.";
    return;
}
$str_length=strlen($_POST['studentCourseDept']);
if($str_length<2 || $str_length >5){
   echo "Sorry, could not validate course department.";
    return;
}

if($_POST["studentCourseNum"] == "" || !is_numeric($_POST['studentCourseNum'])){
	$add_course=false;
    echo "Sorry, could not validate course number.";
    return;
}
$str_length=strlen($_POST['studentCourseNum']);
if($str_length<3 || $str_length >5){
    echo "Sorry, could not validate course number.";
    return;
}

$student_ulink = $_POST['studentULinkID'];
$courseDepartment = $_POST['studentCourseDept'];
$courseNumber = $_POST['studentCourseNum'];
$i_courseNumber = (int) $courseNumber;

//make sure this course does exist
$sql="select id from fb_courses where course_dept='$courseDepartment' and course_num=$i_courseNumber";
$result = $db->Execute($sql);
$course_id = $result->fields['id'];
if(!$course_id)
{
    $add_course=false;
    echo "Sorry, this course doest not exist.";
    return;
}

//we are validated, add student to course
if($add_course){ 
	$sql="insert into fb_reach_student_courses values ('$course_id','$student_ulink')";
	$db->Execute($sql);
    echo "Great success! Added $student_ulink to $courseDepartment $courseNumber.";
}
?>
